﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using KetBanBonPhuong;
using Telerik.Web.UI;

public partial class Admin_UserControls_UserManagement : System.Web.UI.UserControl
{
    private static int UserID = -1;
    private static bool IsAddUser = false;

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            Panel1.Visible = true;
            Panel2.Visible = false;
        }
    }
    protected void RadGrid1_iTemcommand(object source, Telerik.Web.UI.GridCommandEventArgs e)
    {
        if (e.CommandName == "Xem")
        {
            Panel1.Visible = false;
            Panel2.Visible = true;
            IsAddUser = false;
            TextBox_UserName.ReadOnly = true;
            GridDataItem item = (GridDataItem)e.Item;
            UserID = int.Parse(item["user_id"].Text.ToString());
            User user = User.GetInfo(UserID);
            TextBox_UserName.Text = user.UserName;
            TextBox_Password.Attributes.Add("Value", user.Password);
            TextBox_ConfirmPassword.Attributes.Add("Value", user.Password);
            TextBox_Email.Text = user.Email;
            TextBox_FirstName.Text = user.FirstName;
            TextBox_LastName.Text = user.LastName;
            Button_OK.Text = "Cập nhật";
            RadioButtonList1.SelectedIndex = (int)user.Active;
            return;
        }
        if (e.CommandName == "Them")
        {
            IsAddUser = true;
            Panel1.Visible = false;
            Panel2.Visible = true;
            TextBox_UserName.ReadOnly = false;
            Button_OK.Text = "Thêm";
            TextBox_UserName.Text = "";
            TextBox_Password.Attributes.Add("Value", "");
            TextBox_ConfirmPassword.Attributes.Add("Value", "");
            TextBox_Email.Text = "";
            TextBox_FirstName.Text = "";
            TextBox_LastName.Text = "";
            RadioButtonList1.SelectedIndex = 0;
            return;
        }

        if (e.CommandName == "Sua")
        {
            IsAddUser = false;
            Panel1.Visible = false;
            Panel2.Visible = true;
            TextBox_UserName.ReadOnly = true;
            GridDataItem item = (GridDataItem)e.Item;
            UserID = int.Parse(item["user_id"].Text.ToString());
            User user = User.GetInfo(UserID);
            TextBox_UserName.Text = user.UserName;
            TextBox_Password.Attributes.Add("Value", user.Password);
            TextBox_ConfirmPassword.Attributes.Add("Value", user.Password);
            TextBox_Email.Text = user.Email;
            TextBox_FirstName.Text = user.FirstName;
            TextBox_LastName.Text = user.LastName;
            Button_OK.Text = "Cập nhật";
            RadioButtonList1.SelectedIndex = (int)user.Active;
            return;
        }

        if (e.CommandName == "Xoa")
        {
            GridDataItem item = (GridDataItem)e.Item;
            UserID = int.Parse(item["user_id"].Text.ToString());
            User user = User.GetInfo(UserID);
            user.Active = 3;
            User.Update(user);
            return;
        }

    }
    protected void RadGrid1_PreRender(object sender, EventArgs e)
    {
        DataTable dt = User.GetAll();
        dt.Columns.Add("status", typeof(String));
        foreach (DataRow dtRow in dt.Rows)
        {
            dtRow["status"] = StatusConversion.StatusConvert((byte)dtRow["active"]);
        }
        RadGrid1.DataSource = dt;
        RadGrid1.DataBind();
    }
    protected void Button_OK_Click(object sender, EventArgs e)
    {
        if (IsAddUser)
        {
            try
            {
                if (User.GetUserByUserName(TextBox_UserName.Text.Trim()) != -1)
                {
                    Label_Result.Text = "Tên tài khoản đã tồn tại";
                    return;
                }
                if(User.GetUserbyEmail(TextBox_Email.Text.Trim()) != -1)
                {
                    Label_Result.Text = "Đã tồn tại tài khoản với Email này";
                    return;
                }
                User user = new User();
                user.UserName = TextBox_UserName.Text.Trim();
                user.Password = TextBox_Password.Text;
                user.FirstName = TextBox_FirstName.Text.Trim();
                user.LastName = TextBox_LastName.Text.Trim();
                user.Email = TextBox_Email.Text.Trim();
                user.Avatar = "";
                user.Active = (byte)RadioButtonList1.SelectedIndex;
                user.Online = false;
                user.CreateDate = DateTime.Now;
                User.Insert(user);
                Panel1.Visible = true;
                Panel2.Visible = false;
            }
            catch (System.Exception Ex)
            {
                Label_Result.Text = Ex.ToString();
            }
        }
        else
        {
            try
            {
                User user = User.GetInfo(UserID);
                user.UserName = TextBox_UserName.Text.Trim();
                user.Password = TextBox_Password.Text;
                user.FirstName = TextBox_FirstName.Text;
                user.LastName = TextBox_LastName.Text.Trim();
                user.Email = TextBox_Email.Text.Trim();
                user.Avatar = "";
                user.Active = (byte)RadioButtonList1.SelectedIndex;
                user.Online = false;
                User.Update(user);
                Panel1.Visible = true;
                Panel2.Visible = false;
            }
            catch (System.Exception Ex)
            {
                Label_Result.Text = Ex.ToString();
            }
        }
    }
}
